package com.mycontract.suitepro.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.mycontract.suitepro.ProjectConfig
import com.mycontract.suitepro.R
import com.mycontract.suitepro.base.BaseDialogAction
import com.mycontract.suitepro.databinding.DialogContractSuiteAppReviewBinding
import com.mycontract.suitepro.extend.openOnGooglePlayStore
import com.mycontract.suitepro.extend.recordContractSuiteClickContractSuiteEvent
import com.mycontract.suitepro.utils.AppConfigUtil
import com.mycontract.suitepro.utils.analytics.util.AppReviewUtil

class AppReviewDialog : DialogFragment(), BaseDialogAction {

    //星级
    private var mRate = 0

    //显示文案
    private var mContent: String? = ""

    companion object {
        fun showContractSuiteDialog(
            fragmentManager: FragmentManager,
            content: String?
        ) {
            val dialog = AppReviewDialog()
            dialog.mContent = content
            dialog.showNow(fragmentManager, AppReviewDialog::class.java.simpleName)
        }
    }

    lateinit var mContractSuiteViewContractSuiteBinding: DialogContractSuiteAppReviewBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContractSuiteViewContractSuiteBinding =
            DialogContractSuiteAppReviewBinding.inflate(layoutInflater)

        return mContractSuiteViewContractSuiteBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initContractSuiteSubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mContractSuiteViewContractSuiteBinding.textAppReviewTitle.text = mContent

    }

    override fun initContractSuiteSubviews() {
        try {
            mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.setImageResource(R.drawable.contract_suite_app_review_star_no)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.setImageResource(R.drawable.contract_suite_app_review_star_no)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.setImageResource(R.drawable.contract_suite_app_review_star_no)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.setImageResource(R.drawable.contract_suite_app_review_star_no)

                mRate = 1
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.recordContractSuiteClickContractSuiteEvent(
                    "1"
                )

                mContractSuiteViewContractSuiteBinding.textAppReviewRate.setBackgroundResource(R.drawable.contract_suite_app_review_active_enable)
            }

            mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.setImageResource(R.drawable.contract_suite_app_review_star_no)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.setImageResource(R.drawable.contract_suite_app_review_star_no)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.setImageResource(R.drawable.contract_suite_app_review_star_no)

                mRate = 2
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.recordContractSuiteClickContractSuiteEvent(
                    "2"
                )

                mContractSuiteViewContractSuiteBinding.textAppReviewRate.setBackgroundResource(R.drawable.contract_suite_app_review_active_enable)
            }

            mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.setImageResource(R.drawable.contract_suite_app_review_star_no)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.setImageResource(R.drawable.contract_suite_app_review_star_no)

                mRate = 3
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.recordContractSuiteClickContractSuiteEvent(
                    "3"
                )
                mContractSuiteViewContractSuiteBinding.textAppReviewRate.setBackgroundResource(R.drawable.contract_suite_app_review_active_enable)
            }

            mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.setImageResource(R.drawable.contract_suite_app_review_star_no)

                mRate = 4
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.recordContractSuiteClickContractSuiteEvent(
                    "4"
                )
                mContractSuiteViewContractSuiteBinding.textAppReviewRate.setBackgroundResource(R.drawable.contract_suite_app_review_active_enable)
            }

            mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar1.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar2.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar3.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar4.setImageResource(R.drawable.contract_suite_app_review_star_yes)
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.setImageResource(R.drawable.contract_suite_app_review_star_yes)

                mRate = 5
                mContractSuiteViewContractSuiteBinding.imageAppReviewStar5.recordContractSuiteClickContractSuiteEvent(
                    "5"
                )
                mContractSuiteViewContractSuiteBinding.textAppReviewRate.setBackgroundResource(R.drawable.contract_suite_app_review_active_enable)
            }

            mContractSuiteViewContractSuiteBinding.textDialogAppReviewNotNow.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.textDialogAppReviewNotNow.recordContractSuiteClickContractSuiteEvent(
                    mRate.toString()
                )
                dismiss()
            }

            mContractSuiteViewContractSuiteBinding.textAppReviewRate.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.textAppReviewRate.recordContractSuiteClickContractSuiteEvent(
                    mRate.toString()
                )

                if (mRate <= 0) {
                    Toast.makeText(
                        requireContext(),
                        "Please select star",
                        Toast.LENGTH_SHORT
                    ).show()
                    return@setOnClickListener
                }

                //如果大于等于4星，跳转系统评介
                if (mRate >= 4) {
//                    try {
//                        //如果配置了使用系统评介，则走
//                        if (AppConfigUtil.mContractSuiteSysContractSuiteConfig?.customize?.isShowSysReviewipetrpide() == true) {
//                            AppReviewUtil.launchReviewFlow(requireContext() as AppCompatActivity) {
//                                if (!it) {
//                                    //跳转应用市场
//                                    requireContext().openOnGooglePlayStore(ProjectConfig.CONTRACTSUITE_Playstore_Link)
//                                }
//                                dismiss()
//                            }
//                        } else {
//                            //跳转应用市场
//                            requireContext().openOnGooglePlayStore(ProjectConfig.CONTRACTSUITE_Playstore_Link)
//                            dismiss()
//                        }
//                    } catch (e: Exception) {
//                        e.printStackTrace()
//                    }
                } else {
                    //跳转反馈页
//                    (requireContext() as AppCompatActivity).startContractSuiteToContractSuiteNextContractSuiteActivity<FeedbackActivity>()
                }
            }

            mContractSuiteViewContractSuiteBinding.imageDialogAppReviewClose.setOnClickListener {
                mContractSuiteViewContractSuiteBinding.imageDialogAppReviewClose.recordContractSuiteClickContractSuiteEvent(
                    mRate.toString()
                )
                dismiss()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showContractSuiteWithContractSuiteUIContractSuiteTag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {
    }

    override fun setContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener(listener: (Int) -> Unit) {
    }

}